package com.weilele.mvvmlib.sample

import android.animation.ValueAnimator
import android.os.Bundle
import androidx.core.animation.doOnEnd
import com.weilele.base.library.BaseActivity
import com.weilele.base.library.FragmentContainerActivity
import com.weilele.mvvm.utils.activity.navigateTo
import com.weilele.mvvm.utils.vLaout.mvvmMutableTypeAdapter
import com.weilele.mvvmlib.databinding.ActivityMainBinding
import com.weilele.mvvmlib.sample.fg.BlankFragment


class MainActivity : BaseActivity<ActivityMainBinding>() {
    //    private val adapter = mvvmRcvListAdapter(
//        HomeHolder::class.java, datas = mutableListOf(
//            Pair("slideBar,RangSlideBar") {
//                navigateTo<SlideBarActivity>()
//            },
//            Pair("webview") {
//                navigateTo<WebviewActivity>()
//            },
//            Pair("LitePal数据库") {
//                navigateTo<LitePalActivity>()
//            },
//        )
//    )
    private val adapter = mvvmMutableTypeAdapter(
        HomeHolder::class.java,
        HomeHolderTestInt::class.java,
        HomeHolderTest::class.java
    )


    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        initSwipe()
        adapter.refresh(
            mutableListOf(
                Pair("slideBar,RangSlideBar") {
                    navigateTo<SlideBarActivity>()
                },
                Pair("webview") {
                    navigateTo<WebviewActivity>()
                },
                Pair("妹子图片") {
                    FragmentContainerActivity.start(this, BlankFragment::class.java)
                },
                Pair("LitePal数据库") {
                    FragmentContainerActivity.start(this, LitePalActivity::class.java)
                },
            )
        )
    }

//    override fun onCreateContentView(inflater: LayoutInflater, parent: ViewGroup?): View? {
//        return Button(this)
//    }

    private fun initSwipe() {
        mBinding.vLayout.adapter = adapter
        mBinding.root.addOnChildUnConsumedListener { parent, target, unConsumedX, unConsumedY, isScrolling ->
            //0.5为阻力
            val needConsumed = unConsumedY * 0.5f
            if (isScrolling) {
                mBinding.vLayout.translationY = needConsumed
            } else {
//               PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, mBinding.b1.translationY,0f)
                ValueAnimator.ofFloat(needConsumed, 0f)
                    .apply {
                        addUpdateListener {
                            val value = it.animatedValue as Float
                            mBinding.vLayout.translationY = value
                        }
                        duration = 200
                        doOnEnd {
                            mBinding.root.initAll()
                        }
                    }.start()
            }
        }
    }
}